package com.gamex.findit.controller.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.gamex.findit.Constants;
import com.gamex.findit.internal.DefaultConfig;
import com.gamex.findit.model.BaseModel;
import com.gamex.findit.model.CommonCode;
import com.gamex.findit.model.GameItem;
import com.gamex.findit.model.MemberItem;
import com.gamex.findit.service.CommonCodeService;
import com.gamex.findit.service.GameItemService;
import com.gamex.findit.service.MemberItemService;
import com.gamex.findit.util.CommonUtil;
import com.gamex.findit.util.Generator;

@Controller("adminGameItem")
public class GameItemController {
	
	private static final Logger logger = LoggerFactory.getLogger(GameItemController.class);
	
	@Autowired GameItemService gameItemService;
	@Autowired MemberItemService memberItemService;
	@Autowired CommonCodeService commonCodeService;
	@Autowired DefaultConfig config;
	
	/**
	 * @param session
	 * @param request
	 * @param response
	 * @param model
	 * @param memberItem
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/admin/member/memberItemList")
	public String memberItemList( HttpSession session
								, HttpServletRequest request
								, HttpServletResponse response
								, Model model
								, @ModelAttribute MemberItem memberItem) throws Exception{

	int count = memberItemService.getMemberItemCount(memberItem);
	List<MemberItem> result = memberItemService.getMemberItem(memberItem);
	model.addAttribute("list", result);
	model.addAttribute("count", count);
	model.addAttribute("pageNo", memberItem.getPageNo());
	model.addAttribute("pageGroup", memberItem.getPageGroup());
	model.addAttribute("searchParam", memberItem);
	
	return "/item/memberItemList";
	}
	
	/**
	 * @param session
	 * @param request
	 * @param response
	 * @param model
	 * @param memberItem
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/admin/member/addMemberItem")
	public String addMemberItem1( HttpSession session
							   , HttpServletRequest request
							   , HttpServletResponse response
							   , Model model
							   , @ModelAttribute MemberItem memberItem) throws Exception{
		
		memberItemService.insertMemberItem(memberItem);
		
	return "redirect:/admin/member/memberItemList";
	}
	
	/**
	 * @param session
	 * @param request
	 * @param response
	 * @param model
	 * @param gameItem
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/admin/gameItemList")
	public String GameItemList( HttpSession session
							, HttpServletRequest request
							, HttpServletResponse response
							, Model model
							, @ModelAttribute GameItem gameItem) throws Exception{

	int count = gameItemService.getGameItemCount(gameItem);
	List<GameItem> result = gameItemService.getGameItem(gameItem);
	model.addAttribute("list", result);
	model.addAttribute("count", count);
	model.addAttribute("pageNo", gameItem.getPageNo());
	model.addAttribute("searchParam", gameItem);
	return "/item/itemList";
	}
	
	/**
	 * @param session
	 * @param request
	 * @param response
	 * @param model
	 * @param gameItem
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/admin/gameItemView")
	public String viewGameItem( HttpSession session
							, HttpServletRequest request
							, HttpServletResponse response
							, ModelMap model
							, @ModelAttribute GameItem gameItem) throws Exception{
		
		model.addAttribute("view", gameItemService.getGameItemView(gameItem.getItemNo()));
		
		CommonCode code = new CommonCode();
		code.setCodeGroup("ITEM_TYPE");
		model.addAttribute("itemTypes", commonCodeService.getCommonCode(code));
		model.addAttribute("searchParam", gameItem);
	return "/item/itemView";
	}
	
	/**
	 * @param session
	 * @param request
	 * @param response
	 * @param model
	 * @param file
	 * @param gameItem
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/admin/addGameItem")
	public String addGameItem( HttpSession session
						   , HttpServletRequest request
						   , HttpServletResponse response
						   , Model model
						   , @RequestParam("file") MultipartFile file
						   , @ModelAttribute GameItem gameItem) throws Exception{
		
		String name = Generator.generateFileName();
		if(file.getSize() > 0){
			CommonUtil.writeImage(request, file, Constants.DEFAULT_ITEM_IMG_PATH, name);
			gameItem.setImg(String.format(Constants.DEFAULT_ITEM_URL, config.getHostAddress()) + name);
		}
		
		gameItemService.insertGameItem(gameItem);
		
	return "redirect:/admin/gameItemList";
	}
	
	/**
	 * @param session
	 * @param request
	 * @param response
	 * @param model
	 * @param file
	 * @param gameItem
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/admin/updateGameItem.*")
	public String updateGameItem( HttpSession session
							  , HttpServletRequest request
							  , HttpServletResponse response
							  , Model model
							  , @RequestParam("file") MultipartFile file
							  , @ModelAttribute GameItem gameItem) throws Exception{
		
		String name = Generator.generateFileName();
		if(file.getSize() > 0){
			CommonUtil.writeImage(request, file, Constants.DEFAULT_ITEM_IMG_PATH, name);
			gameItem.setImg(String.format(Constants.DEFAULT_ITEM_URL, config.getHostAddress()) + name);
		}
		
		gameItemService.updateGameItem(gameItem);
		
	return "redirect:/admin/gameItemList";
	}
	
	/**
	 * @param session
	 * @param request
	 * @param response
	 * @param model
	 * @param gameItem
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/admin/deleteGameItem")
	public String deleteGameItem( HttpSession session
							  , HttpServletRequest request
							  , HttpServletResponse response
							  , Model model
							  , @ModelAttribute GameItem gameItem) throws Exception{
		
		memberItemService.deleteMemberItemByItemNo(gameItem.getItemNo());
		gameItemService.deleteGameItem(gameItem.getItemNo());
		
	return "redirect:/admin/gameItemList";
	}
	
}
